package cn.momoshenchi.coupon.service.impl;

import cn.momoshenchi.common.to.MemberPrice;
import cn.momoshenchi.common.to.SkuReductionTo;
import cn.momoshenchi.coupon.entity.MemberPriceEntity;
import cn.momoshenchi.coupon.entity.SkuLadderEntity;
import cn.momoshenchi.coupon.service.MemberPriceService;
import cn.momoshenchi.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.momoshenchi.common.utils.PageUtils;
import cn.momoshenchi.common.utils.Query;

import cn.momoshenchi.coupon.dao.SkuFullReductionDao;
import cn.momoshenchi.coupon.entity.SkuFullReductionEntity;
import cn.momoshenchi.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
@Autowired
private SkuLadderService skuLadderService;

@Autowired
private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo)
    {
        //1.满减打折,会员价
        SkuLadderEntity skuLadderEntity=new SkuLadderEntity();
        BeanUtils.copyProperties(reductionTo,skuLadderEntity);
        skuLadderEntity.setAddOther(reductionTo.getCountStatus());
        if(reductionTo.getFullCount()>0)
        {
            skuLadderService.save(skuLadderEntity);
        }
        //2.满减
        SkuFullReductionEntity skuFullReductionEntity =new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo,skuFullReductionEntity);
        if(skuFullReductionEntity.getFullPrice().compareTo(BigDecimal.ZERO)>0)
        {
            save(skuFullReductionEntity);}


        //3.会员价
        List<MemberPrice> memberPrice = reductionTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(reductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->item.getMemberPrice().compareTo(BigDecimal.ZERO)>0).collect(Collectors.toList());
         memberPriceService.saveBatch(memberPriceEntities);

    }

}